/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fontys.time;

/**
 *
 * @author Administrator
 */
public class Period2 implements IPeriod {

    private ITime beginTime;
    private long duration;
    
    public Period2(ITime bt, ITime et){
        if (bt.compareTo(et) > 0)
            throw new IllegalArgumentException("Begin time larger"
                    + " than end time");
        beginTime = bt;
        duration = beginTime.difference(et);
        
    }
    
    @Override
    public ITime getBeginTime() {
        return beginTime;
    }

    @Override
    public ITime getEndTime() {
        return beginTime.plus((int)duration);
    }

    @Override
    public int length() {
        return (int)duration;
    }

    @Override
    public void setBeginTime(ITime beginTime) {
        if (beginTime.compareTo(this.getEndTime()) > 0)
            throw new IllegalArgumentException("Begin time larger"
                    + " than end time");
        this.beginTime = beginTime;
    }

    @Override
    public void setEndTime(ITime endTime) {
        if (this.beginTime.compareTo(endTime) > 0)
            throw new IllegalArgumentException("Begin time larger"
                    + " than end time");
        this.duration = endTime.difference(this.getBeginTime());
    }

    @Override
    public void move(int minutes) {
        this.beginTime.plus(minutes);
        this.duration += minutes;
    }

    @Override
    public void changeLengthWith(int minutes) {
        long m = this.duration + minutes;
        if (m<0)
           throw new IllegalArgumentException("Begin time larger"
                    + " than end time");
        this.duration = m;
    }

    @Override
    public boolean isPartOf(IPeriod period) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public IPeriod unionWith(IPeriod period) {
        if (this.intersectionWith(period) == null)
            return null;
        ITime beginA = this.getBeginTime();
        ITime endA = this.getEndTime();
        ITime beginB = period.getBeginTime();
        ITime endB = period.getEndTime();
        if (beginA.compareTo(beginB)>=0)
        {
            if (endA.compareTo(endB) >= 0)
                return new Period2(beginB, endA);
            return new Period2(beginB, endB);
        }
        else
        {
           if (endA.compareTo(endB) >= 0)
              return new Period2(beginA, endA);
           return new Period2(beginA, endB);
        }
    }

    @Override
    public IPeriod intersectionWith(IPeriod period) {
        ITime beginA = this.getBeginTime();
        ITime endA = this.getEndTime();
        ITime beginB = period.getBeginTime();
        ITime endB = period.getEndTime();
        //a >= b
        if (beginA.compareTo(beginB)>=0)
        {
            if (endA.compareTo(endB) >= 0 && beginA.compareTo(endB) <= 0)
            {
               return new Period(beginA, endB); 
            }
            else if (endA.compareTo(endB)<=0)
            {
                return new Period(beginA, endA);
            }
        }
        else //b<a
        {
            if (endB.compareTo(endA) >= 0 && beginB.compareTo(endA) <= 0)
            {
               return new Period(beginB, endA); 
            }
            else if (endB.compareTo(endA)<=0)
            {
                return new Period(beginB, endB);
            }
        }
        return null;
    }
    
}
